﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Cmd;
using GameData;
using UnityEngine.UI;

public class SetUI : MonoBehaviour
{

    public ToggleGroup ToggleGroup;
   public List<Toggle> Toggles = new List<Toggle>();
    public Toggle NumlindToggle;
    public GameObject testPickAllNumer;

    //
    public GameObject allClearBtn;
    public GameObject backPickBtn;
    //
    public Toggle StartToggle;

    // Use this for initialization
    void Start ()
	{


        int maxcount = GlobalVO.GetInstance().NumAllInfo.batchList.Count;
        Debug.Log("==========数量：" + maxcount);
        for (int i = 0; i < Toggles.Count; i++)
        {
            Toggle toggle = Toggles[i];
            if (i < maxcount)
            {
                Text tx = toggle.transform.FindChild("Label").GetComponent<Text>();
                tx.text = GlobalVO.GetInstance().NumAllInfo.batchList[i];
                //设置
                if (GlobalVO.GetInstance().NumAllInfo.SetDataDic["批次"] == tx.text)
                {
                    toggle.isOn = true;
                }
                //
                toggle.onValueChanged.AddListener(onValueChange);
                //

            }
            else
            {
                toggle.gameObject.SetActive(false);
            }

        }

        NumlindToggle.isOn = GlobalVO.GetInstance().NumAllInfo.SetDataDic["连号"] == "是";
        NumlindToggle.onValueChanged.AddListener(OnValueNumLineChange);
        //

        StartToggle.isOn = GlobalVO.GetInstance().NumAllInfo.SetDataDic["开始抽号"] == "是";
        StartToggle.onValueChanged.AddListener(OnStartCHouChange);


        EventTriggerListener.Get(testPickAllNumer).onClick = OntestPickAllNumer;
        EventTriggerListener.Get(allClearBtn).onClick = OnallClearBtn;
        EventTriggerListener.Get(backPickBtn).onClick = OnbackPickBtn;






    }

    void OnNumberInfo(NumAllInfo numallInfo)
    {
        GlobalVO.GetInstance().NumAllInfo = numallInfo;



    }


    public void OntestPickAllNumer(GameObject obj)
    {
        //
        TestGetAllNumer ts = new TestGetAllNumer();
        ts.Send(GlobalVO.GetInstance().roleInfo.UserInfo,OnTestPIckNUm);

    }

    public void OnallClearBtn(GameObject obj)
    {
        ClearPickNum cp = new ClearPickNum();
        DataInfo di =  new DataInfo();
        di.valueStr = "完全清空";
        cp.Send(di, OnresClear);

    }



    public void OnbackPickBtn(GameObject obj)
    {
        ClearPickNum cp = new ClearPickNum();
        DataInfo di = new DataInfo();
        di.valueStr = "后退一步";
        cp.Send(di, OnresClear);

    }

    public void OnresClear(PickNumInfo pickNumInfo)
    {


    }


    public void OnTestPIckNUm(ResponserData responserData)
    {
        if (responserData.result == "OK")
        {
            Debug.Log("领取完毕");
        }

    }

    public void OnValueNumLineChange(bool res)
    {
        
        SetDataInfo setDataInfo = new SetDataInfo();
        DataInfo dataInfo = new DataInfo();
        dataInfo.dataType = "连号";
        dataInfo.valueStr = "否";
        if (res)
        {
            dataInfo.valueStr = "是";
        }

        setDataInfo.Send(dataInfo, OnResponse);
    }


    public void OnStartCHouChange(bool res)
    {
        SetDataInfo setDataInfo = new SetDataInfo();
        DataInfo dataInfo = new DataInfo();
        dataInfo.dataType = "开始抽号";
        dataInfo.valueStr = "否";
        if (res)
        {
            dataInfo.valueStr = "是";
        }
        setDataInfo.Send(dataInfo, OnResponse);
        //
    }


    public void onValueChange(bool res)
    {
        if (res)
        {
            string key = "";
            for (int i = 0; i < Toggles.Count; i++)
            {
                Toggle toggle = Toggles[i];
                key= toggle.transform.FindChild("Label").GetComponent<Text>().text;
                if (toggle.isOn)
                {
                    break;
                }
            }
            SetDataInfo setDataInfo = new SetDataInfo();
            DataInfo dataInfo = new DataInfo();
            dataInfo.dataType = "批次";
            dataInfo.valueStr = key;
            setDataInfo.Send(dataInfo, OnResponse);
        }
    }

    void OnResponse(DataInfo res)
    {
        
    }

    public void SetOn(string str)
    {
        for (int i = 0; i < Toggles.Count; i++)
        {
            Toggle toggle = Toggles[i];
            Text tx = toggle.transform.FindChild("Label").GetComponent<Text>();
            if (tx.text == str)
            {
                if (!toggle.isOn)
                {
                    toggle.isOn = true;
                }
                
                break;
            }
        }

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="str"></param>
    public void SetLineNum(string str)
    {
        if (str == "是")
        {
            if (!NumlindToggle.isOn)
            {
                NumlindToggle.isOn = true;
            }
        }
        else
        {
            if (NumlindToggle.isOn)
            {
                NumlindToggle.isOn = false;
            }
        }
    }

    

	// Update is called once per frame
	void Update () {
	
	}
}
